import matplotlib.pyplot as plt
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import numpy as np
from PIL import Image
import matplotlib.cbook as cbook
from pylab import plotfile, gca, show
import os
import blockspring


class PlotWordCloud(object):
    """
    词云
    """
    def __init__(self):
        self.text = """
        Project Gutenberg's Alice's Adventures in Wonderland, by Lewis Carroll
        
        This eBook is for the use of anyone anywhere at no cost and with
        almost no restrictions whatsoever.  You may copy it, give it away or
        re-use it under the terms of the Project Gutenberg License included
        with this eBook or online at www.gutenberg.org
        """

    def plot_base(self):
        """
        全量词云
        :return:
        """
        # wc = WordCloud(background_color='white', stopwords='said', max_font_size=40,
        #                random_state=42)
        wc = WordCloud(background_color='white', max_font_size=40)

        # 生成词云, 可以用generate输入全部文本(中文不好分词),
        # 也可以我们计算好词频后使用generate_from_frequencies函数
        wc.generate(self.text)

        plt.imshow(wc)
        plt.axis('off')

        plt.show()

    def plot_circle(self):
        """
        原型词云
        :return:
        """
        x, y = np.ogrid[: 300, : 300]
        mask = np.power(x - 150, 2) + np.power(y - 150, 2) > np.power(130, 2)
        mask = 255 * mask.astype(int)

        wc = WordCloud(background_color='white', mask=mask)
        wc.generate(self.text)

        plt.axis('off')
        plt.imshow(wc, interpolation='bilinear')
        plt.show()

    def plot_pic(self):
        """
        背景图片
        :return:
        """
        pic_mask = np.array(Image.open('./data/boy.jpg'))
        stopwords = set(STOPWORDS)
        stopwords.add('said')

        wc = WordCloud(background_color='white', max_words=1000, mask=pic_mask)
        wc.generate(self.text)

        plt.imshow(wc, interpolation='bilinear')
        plt.axis('off')

        plt.show()


class PlotStock(object):
    """
    股票
    """
    @staticmethod
    def plot_stock():
        """
        股票
        :return:
        """
        file = os.path.join(os.getcwd(), './data/603816.csv')
        # fname = cbook.get_sample_data(file, asfileobj=False)
        # print(fname)
        # plotfile(file, ('date', 'high', 'low', 'close'), subplots=False)  # subplots=False不用子图
        plotfile(file, (0, 3, 5), plotfuncs={5: 'bar'})

        show()

    @staticmethod
    def d3():
        """
        blockspring: error(依赖国外网站数据)
        :return:
        """
        res = blockspring.runParsed('stock-price-comparison',
                                       {'tickers': 'FB, LNKD, TWTR, MSFT',
                                        'start_date': '2019-01-01',
                                        'end_date': '2019-09-01'})
        print(res)





# 词云
# wordcloud = PlotWordCloud()
# 全量词云
# wordcloud.plot_base()
# 原型词云
# wordcloud.plot_circle()
# 图片
# wordcloud.plot_pic()

# 股票
# plot_stock = PlotStock()
# 常规
# plot_stock.plot_stock()
# plot_stock.d3()
